package com.shizhanzhe.gxb.utils;


import android.content.Context;
import android.os.Handler;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by hasee on 2016/11/1.
 */
public class OkHttpDownloadJsonUtil {
    public interface onOkHttpDownloadListener {
        void onsendJson(String json);
    }

    public static void downloadJson(final Context context, final String path, final onOkHttpDownloadListener listener) {
        final Handler handler = new Handler();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(path).build();
        final Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (e.toString().contains("SocketTimeoutException")) {//网络超时
                            listener.onsendJson("1");
                        } else if (e.toString().contains("UnknownHostException")) {//网络异常
                            listener.onsendJson("2");
                        }else{
                            listener.onsendJson(e.toString());
                        }
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String str = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listener.onsendJson(str);
                    }
                });
            }
        });
    }
}


